package gui;

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import code.KeyStoreManager;
import code.Language;
import code.LogEncryption;
import code.Settings;
import code.SymmetricKeyMaker;

public class DataIntegrityCreateKey extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JRadioButton jRadioButton = null;
	private JRadioButton jRadioButton1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField = null;
	private JLabel jLabel3 = null;
	private JPasswordField jPasswordField = null;
	private JLabel jLabel4 = null;
	private JPasswordField jPasswordField1 = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	ButtonGroup rbgroup = new ButtonGroup();
	ATOCOmain atocoframe;
	DataIntegrityCreateFile prevclass;
	private KeyStoreManager ksm;
	private HashMap labels;
	private Settings s;
	
	/**
	 * This is the default constructor
	 */
	public DataIntegrityCreateKey(ATOCOmain atocoframe, DataIntegrityCreateFile prevclass) {
		super();
		Language l = new Language();
		s= new Settings();
		labels = l.changeLanguage((String) s.global.get("language"), "DataIntegrityCreateKey", (String) s.global.get("installPath"));

		this.atocoframe=atocoframe;
		this.prevclass=prevclass;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel4 = new JLabel();
		jLabel4.setBounds(new Rectangle(15, 136, 128, 16));
		jLabel4.setText((String) labels.get("jLabel4"));
		jLabel3 = new JLabel();
		jLabel3.setBounds(new Rectangle(15, 105, 73, 16));
		jLabel3.setText((String) labels.get("jLabel3"));
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(15, 74, 38, 16));
		jLabel2.setText((String) labels.get("jLabel2"));
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(16, 47, 88, 16));
		jLabel1.setText((String) labels.get("jLabel1"));
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(15, 15, 377, 16));
		jLabel.setText((String) labels.get("jLabel"));
		this.setSize(652, 366);
		this.setLayout(null);
		this.setBackground(Color.WHITE);
		this.add(jLabel, null);
		this.add(jLabel1, null);
		this.add(getJRadioButton(), null);
		this.add(getJRadioButton1(), null);
		this.add(jLabel2, null);
		this.add(getJTextField(), null);
		this.add(jLabel3, null);
		this.add(getJPasswordField(), null);
		this.add(jLabel4, null);
		this.add(getJPasswordField1(), null);
		this.add(getJButton(), null);
		this.add(getJButton1(), null);
	}

	/**
	 * This method initializes jRadioButton	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setBounds(new Rectangle(145, 47, 94, 21));
			jRadioButton.setSelected(true);
			jRadioButton.setText("HMacSHA1");
			prevclass.mc.algorithm=jRadioButton.getText();
			jRadioButton.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton.isSelected())
					{
						prevclass.mc.algorithm=jRadioButton.getText();
					}

				}
			});
			rbgroup.add(jRadioButton);
		}
		return jRadioButton;
	}

	/**
	 * This method initializes jRadioButton1	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setBounds(new Rectangle(245, 47, 82, 21));
			jRadioButton1.setText("HMacMD5");
			jRadioButton1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton1.isSelected())
					{
						prevclass.mc.algorithm=jRadioButton1.getText();
					}

				}
			});
			rbgroup.add(jRadioButton1);
		}
		return jRadioButton1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(150, 74, 152, 20));
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(150, 106, 151, 20));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jPasswordField1	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField1() {
		if (jPasswordField1 == null) {
			jPasswordField1 = new JPasswordField();
			jPasswordField1.setBounds(new Rectangle(151, 135, 150, 20));
		}
		return jPasswordField1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(193, 195, 109, 37));
			jButton.setText((String) labels.get("jButton"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					if(!prevclass.mc.algorithm.equals(null)){
						
						prevclass.mc.generateKey(prevclass.mc.algorithm);

						ksm = new KeyStoreManager(
								(String)Settings.global.get("personalKeyStoreFile"), 
								(String)Settings.global.get("personalKeyStorePassword"));
						if (ksm.getKeyStore() == null) {
							System.out.println("Keystore file not found. Creating one.");
							ksm.createKeyStore(
									(String)Settings.global.get("personalKeyStoreFile"), 
									(String)Settings.global.get("personalKeyStorePassword"));
						}
						ksm.saveSecretKey(jTextField.getText(),prevclass.mc.getKey(), jPasswordField.getText());
						ksm.saveKeyStore();
						prevclass.mc.keyalias = jTextField.getText();
						LogEncryption log=new LogEncryption();
						log.initiateKeylist();
						log.addtoKeylist(jTextField.getText(), prevclass.mc.getKey().getAlgorithm());
						System.out.println("key alias: "+jTextField.getText());

						atocoframe.contentPane(prevclass);
						

					}

				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(77, 195, 109, 37));
			jButton1.setText((String) labels.get("jButton1"));
		}
		return jButton1;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
